package cn.lopr.im.android.mvp.view.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.utils.DateUtil;
import de.hdodenhof.circleimageview.CircleImageView;

public class CommunityArticleCommentAdapter extends RecyclerView.Adapter<CommunityArticleCommentAdapter.ViewHolder> {

    private final List<ArticleCommentVo> articleCommentVos;
    private final Context context;
    private CommunityArticleCommentAdapter.OnRecyclerViewItemClickListener mOnItemClickListener;

    public CommunityArticleCommentAdapter(Context context, List<ArticleCommentVo> articleCommentVos) {
        this.articleCommentVos = articleCommentVos;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_community_article_comment, parent, false);

        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.articleCommentVo = this.articleCommentVos.get(position);

        holder.tvComment.setText(holder.articleCommentVo.getComment());
        holder.tvNickname.setText(holder.articleCommentVo.getCurrentUser().getNickname());
        holder.tvCommentPublishTime.setText(DateUtil.formatHMS(holder.articleCommentVo.getTime()));
        holder.tvCommentFloor.setText("第" + (position + 1) + "楼");
        holder.tvCommentReply.setVisibility(View.GONE);

        if (mOnItemClickListener != null) {
            holder.llPublishCommentUser.setTag(holder.articleCommentVo);
            holder.llPublishCommentUser.setOnClickListener(v -> mOnItemClickListener.onItemClick(v, position));
            holder.ivReplyComment.setTag(holder.articleCommentVo);
            holder.ivReplyComment.setOnClickListener(view -> mOnItemClickListener.onItemClick(view, position));
            holder.tvReplyAll.setTag(holder.articleCommentVo);
            holder.tvReplyAll.setOnClickListener(view -> mOnItemClickListener.onItemClick(view, position));
        }

        if (holder.articleCommentVo.getChildArticleCommentVos() == null || holder.articleCommentVo.getChildArticleCommentVos().size() == 0) {
            holder.flCommentReply.setVisibility(View.GONE);
        } else {
            List<ArticleCommentVo> childArticleCommentVos = holder.articleCommentVo.getChildArticleCommentVos();
            for (int i = 0; i < childArticleCommentVos.size(); i++) {
                ArticleCommentVo childArticleCommentVo = childArticleCommentVos.get(i);
                holder.tvCommentReplyUsernames[i].setText(childArticleCommentVo.getCurrentUser().getNickname() + "：");
                holder.tvCommentReplyUsernames[i].setVisibility(View.VISIBLE);
                holder.tvCommentReplyContents[i].setText(childArticleCommentVo.getComment());
                holder.tvCommentReplyContents[i].setVisibility(View.VISIBLE);
            }
            if (holder.articleCommentVo.getChildArticleCommentTotal() > 2) {
                holder.tvReplyAll.setText("查看全部" + holder.articleCommentVo.getChildArticleCommentTotal() + "条评论");
                holder.tvReplyAll.setVisibility(View.VISIBLE);
            }

        }

        Glide.with(context).load(holder.articleCommentVo.getCurrentUser().getPortrait())
                .placeholder(R.drawable.default_head_portrait)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        holder.ivPortrait.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        holder.ivPortrait.setImageDrawable(errorDrawable);
                    }
                });
    }

    @Override
    public int getItemCount() {
        return articleCommentVos == null ? 0 : articleCommentVos.size();
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int section);

        void onItemLongClick(View view, int section);
    }

    public void onRecyclerViewItemClickListener(OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View mView;
        ArticleCommentVo articleCommentVo;
        CircleImageView ivPortrait;
        TextView tvNickname;
        TextView tvPublishTime;
        TextView tvComment;
        TextView tvCommentPublishTime;
        FrameLayout flCommentReply;
        ImageView ivReplyComment;
        LinearLayout llPublishCommentUser;
        TextView tvCommentFloor;
        TextView tvReplyAll;
        TextView tvCommentReply;
        TextView[] tvCommentReplyUsernames;
        TextView[] tvCommentReplyContents;

        public ViewHolder(View view) {
            super(view);
            this.mView = view;
            this.ivPortrait = (CircleImageView) view.findViewById(R.id.iv_portrait);
            this.tvNickname = (TextView) view.findViewById(R.id.tv_nickname);
            this.tvPublishTime = (TextView) view.findViewById(R.id.tv_publish_time);
            this.tvComment = (TextView) view.findViewById(R.id.tv_comment);
            this.tvCommentPublishTime = (TextView) view.findViewById(R.id.tv_comment_publish_time);
            this.flCommentReply = (FrameLayout) view.findViewById(R.id.fl_comment_reply);
            this.ivReplyComment = (ImageView) view.findViewById(R.id.iv_reply_comment);
            this.llPublishCommentUser = (LinearLayout) view.findViewById(R.id.ll_publish_comment_user);
            this.tvCommentFloor = (TextView) view.findViewById(R.id.tv_comment_floor);
            this.tvReplyAll = (TextView) view.findViewById(R.id.tv_reply_all);
            this.tvCommentReplyUsernames = new TextView[]{view.findViewById(R.id.tv_comment_reply_username_1), view.findViewById(R.id.tv_comment_reply_username_2)};
            this.tvCommentReplyContents = new TextView[]{view.findViewById(R.id.tv_comment_reply_content_1), view.findViewById(R.id.tv_comment_reply_content_2)};
            this.tvCommentReply = view.findViewById(R.id.tv_comment_reply);
        }

    }
}
